#!/usr/bin/env bash

if [[ "$OSTYPE" == "darwin"* ]]; then
	realpath() { [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"; }
	ROOT=$(dirname "$(dirname "$(realpath "$0")")")
else
	ROOT=$(dirname "$(dirname "$(readlink -f $0)")")
	# If the script is running in Docker using the WSL2 engine, powershell.exe won't exist
	if grep -qi Microsoft /proc/version && type powershell.exe > /dev/null 2>&1; then
		IN_WSL=true
	fi
fi

function build() {
	cd "$ROOT"

	# Configuration
	export ENVIRONMENT=development

	# Cleanup
	rm -rf "build"

	# CMake build
	mkdir build && cd build && cmake ..
	make -j16

	./VideoTool
}

if [ "$IN_WSL" == "true" ] && [ -z "$DISPLAY" ]; then
	echo "WSL Mode Unsupported."
else
	build "$@"
fi

exit $?